package factory_pattern.simple_factory;

import factory_pattern.simple_factory.connection_impl.NetworkConnection;

public class Client {
    public static void main(String[] args) {
        // 场景1：TCP连接硬件设备
        NetworkConnection tcpConn = ConnectionFactory.createConnection("tcp", "192.168.1.100", 8080);
        if (tcpConn.connect()) {
            tcpConn.sendData("control: start_device".getBytes());
            tcpConn.close();
        }

        System.out.println("----------------------------------------");

        // 场景2：UDP发送日志
        NetworkConnection udpConn = ConnectionFactory.createConnection("udp", "10.0.0.5", 514);
        if (udpConn.connect()) {
            udpConn.sendData("[INFO] 服务启动成功：2024-05-20 10:30:00".getBytes());
            udpConn.close();
        }

        System.out.println("----------------------------------------");

        // 场景3：WebSocket推送实时数据
        NetworkConnection wsConn = ConnectionFactory.createConnection("ws", "ws://127.0.0.1:8765");
        if (wsConn.connect()) {
            wsConn.sendData("{\"user_count\": 128, \"server_load\": 0.35}".getBytes());
            wsConn.close();
        }
    }
}